package com.tempjob.common.businesss.service;

import com.alibaba.fastjson.JSON;
import com.tempjob.common.model.PositionModel;
import com.tempjob.common.model.ReGeo;
import com.tempjob.common.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author hui
 * @Date 2024/12/5 17:01
 **/
@Slf4j
@Component
public class AddressService {

    private final static String gdKey = "141d49a7950df086885505504aea7e68";

    public static PositionModel getAddress(Double longitude, Double latitude) {
        String location = longitude + "," + latitude;
        String gaodeStreetApi = "https://restapi.amap.com/v3/geocode/regeo?key=" + gdKey + "&location=" + location;
        String reGeoResult = HttpUtils.sendGet(gaodeStreetApi, null, null);
        log.info("经纬度分析地址:{},{},{}", location, reGeoResult);
        if (StringUtils.isNotEmpty(reGeoResult)) {
            ReGeo reGeo = JSON.parseObject(reGeoResult, ReGeo.class);
            if (reGeo.getStatus().equals(1)) {
                PositionModel positionModel = new PositionModel();
                positionModel.setFormattedAddress(reGeo.getRegeocode().getFormatted_address());
                positionModel.setCountry(reGeo.getRegeocode().getAddressComponent().getCountry());
                positionModel.setProvince(reGeo.getRegeocode().getAddressComponent().getProvince());
                positionModel.setCity(reGeo.getRegeocode().getAddressComponent().getCity());
                positionModel.setCitycode(reGeo.getRegeocode().getAddressComponent().getCitycode());
                positionModel.setDistrict(reGeo.getRegeocode().getAddressComponent().getDistrict());
                positionModel.setAdcode(reGeo.getRegeocode().getAddressComponent().getAdcode());
                positionModel.setTownship(reGeo.getRegeocode().getAddressComponent().getTownship());
                positionModel.setTowncode(reGeo.getRegeocode().getAddressComponent().getTowncode());
                positionModel.setLongitude(longitude);
                positionModel.setLatitude(latitude);
                return positionModel;
            }
        }
        return null;

    }


}
